<template>
  <div>
    <el-dialog v-model="state.showDialog" destroy-on-close :title="title" draggable :close-on-click-modal="false"
      :close-on-press-escape="false" width="569px">
      <el-form ref="formRef" :model="form" size="default" label-width="80px">
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="产线代号" prop="lineCode"
              :rules="[{ required: true, message: '请输入产线代号', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.lineCode" placeholder="请输入产线代号" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="产线名称" prop="lineName"
              :rules="[{ required: true, message: '请输入产线名称', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.lineName" placeholder="请输入产线名称" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="产线类型" prop="lineType"
              :rules="[{ required: false, message: '请输入产线类型', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.lineType" placeholder="请输入产线类型" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="小时产量" prop="capacity"
              :rules="[{ required: false, message: '请输入小时产量', trigger: ['blur', 'change'] }]">
              <el-input type="number" v-model="form.capacity" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="班次时长" prop="shiftDuration"
              :rules="[{ required: false, message: '请输入班次时长', trigger: ['blur', 'change'] }]">
              <el-input type="number" v-model="form.shiftDuration" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="周时长" prop="workHours"
              :rules="[{ required: false, message: '请输入周时长', trigger: ['blur', 'change'] }]">
              <el-input type="number" v-model="form.workHours" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="班次数量" prop="shiftCount"
              :rules="[{ required: false, message: '请输入产线类型', trigger: ['blur', 'change'] }]">
              <el-input type="number" v-model="form.shiftCount" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="启用时间" prop="startTime"
              :rules="[{ required: false, message: '请输入启用时间', trigger: ['blur', 'change'] }]">
              <!-- <el-input type="date" v-model="form.startTime" autocomplete="off" /> -->
              <el-date-picker v-model="form.startTime" type="datetime" placeholder="选择日期" style="width:48%" />
            </el-form-item>

          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="停用时间" prop="endTime"
              :rules="[{ required: false, message: '请输入停用时间', trigger: ['blur', 'change'] }]">
              <el-date-picker v-model="form.endTime" type="datetime" placeholder="选择日期" style="width: 48%" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" @click="onSure" size="default" :loading="state.sureLoading">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts" name="industry/production-line/form">
import { reactive, toRefs, getCurrentInstance, ref } from 'vue'
import { ProductionLineGetListOutput, ProductionLineAddInput, ProductionLineUpdateInput } from '/@/api/industry/data-contracts'
import { ProductionLineApi } from '/@/api/industry/ProductionLine'
import eventBus from '/@/utils/mitt'

defineProps({
  title: {
    type: String,
    default: '',
  },
})
const { proxy } = getCurrentInstance() as any
const formRef = ref()
const state = reactive({
  showDialog: false,
  sureLoading: false,
  ProductionLineData: [] as ProductionLineGetListOutput[],
  form: {} as ProductionLineAddInput & ProductionLineUpdateInput,
})
const { form } = toRefs(state)

// 打开对话框
const open = async (row: any = {}) => {
  if (row.id > 0) {
    const res = await new ProductionLineApi().get({ id: row.id }, { loading: true }).catch(() => {
      proxy.$modal.closeLoading()
    })

    if (res?.success) {
      state.form = res.data as ProductionLineAddInput & ProductionLineUpdateInput
    }
  } else {
    state.form = { lineCode: '', lineName: '' } as ProductionLineAddInput & ProductionLineUpdateInput
  }
  state.showDialog = true
}
// 确定
const onSure = () => {
  formRef.value.validate(async (valid: boolean) => {
    if (!valid) return

    state.sureLoading = true
    let res = {} as any
    if (state.form.id != undefined && state.form.id > 0) {
      res = await new ProductionLineApi().update(state.form, { showSuccessMessage: true }).catch(() => {
        state.sureLoading = false
      })
    } else {
      res = await new ProductionLineApi().add(state.form, { showSuccessMessage: true }).catch(() => {
        state.sureLoading = false
      })
    }
    state.sureLoading = false

    if (res?.success) {
      eventBus.emit('refreshProductionLine')
      state.showDialog = false
    }
  })
}

// 取消
const onCancel = () => {
  state.showDialog = false
}
defineExpose({
  open,
})
</script>